package softswitch.ws;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import softswitch.model.CallDetail;
import softswitch.model.CallSummaryRouteGroup;
import softswitch.model.Offer;
import softswitch.model.OfferDetail;
import softswitch.model.Package;
import softswitch.model.PackageSummary;
import softswitch.model.Route;
import softswitch.service.CallDetailService;
import softswitch.service.CallSummaryRouteGroupService;
import softswitch.service.PackageSummaryService;
import util.DateUtil;


@Stateless
public class CallSummaryService {
	
	@EJB
	SoftswitchService softswitchService;
	
	@EJB
	CallDetailService callDetailService;
	
	@EJB
	PackageSummaryService packageSummaryService;
	
	@EJB
	CallSummaryRouteGroupService callSummaryRouteGroupService;
	
	
	DateUtil  dateUtil = new DateUtil();
	CallLogger callLogger = new CallLogger();
	

	public CallDetail updatePackageSummary(CallDetail calldetail) {
						
		List<Package> packags = softswitchService.listPackagesByCircuit(calldetail.getCircuit());
		if(packags == null || packags.isEmpty()){
			callLogger.calllog(calldetail, "ERROR", "calldetailJOB", "No Packages Found");
		}
		else{
			
			Iterator<Package> packagsIt = packags.iterator();
			while(packagsIt.hasNext()){
				
				Package packag = packagsIt.next();
				
				Offer offer = packag.getOffer();
				OfferDetail oferdetail = softswitchService.getOfferDetailByOfferRoute(offer,calldetail.getRoute());
			
				if(oferdetail!=null){
					
					callLogger.calllog(calldetail, "DEBUG", "calldetailJOB", "Package Found: " + packag.getOffer().getName() );
					callLogger.calllog(calldetail, "DEBUG", "calldetailJOB", "Package Minutes: " + packag.getOffer().getMinutes());
					
					Date month = dateUtil.round2Month(calldetail.getCalldate());
					Date day = dateUtil.round2Day(calldetail.getCalldate());
					
					PackageSummary packageSummaryMonth = packageSummaryService.getByDate(packag, month);
					
					if(packageSummaryMonth == null){
						callLogger.calllog(calldetail, "DEBUG", "calldetailJOB", "New Package Summary Cycle: " );
						callLogger.calllog(calldetail, "DEBUG", "calldetailJOB", "Package Minutes Expended: " + 0);
						callLogger.calllog(calldetail, "DEBUG", "calldetailJOB", "Package Minutes getRemaining: " + packag.getOffer().getMinutes());
						packageSummaryMonth = new PackageSummary();
						packageSummaryMonth.setCircuit(calldetail.getCircuit());
						packageSummaryMonth.setPackag(packag);
						packageSummaryMonth.setCalldate(month);
						packageSummaryMonth.setExpended(calldetail.getTime());
						packageSummaryMonth.setRemaining(packag.getOffer().getMinutes().intValue()*60);
						packageSummaryService.save(packageSummaryMonth);
					}
					else{
						callLogger.calllog(calldetail, "DEBUG", "calldetailJOB", "Package Minutes Expended: " + packageSummaryMonth.getExpended()/60);
						callLogger.calllog(calldetail, "DEBUG", "calldetailJOB", "Package Minutes getRemaining: " + packageSummaryMonth.getRemaining()/60);
	
						if(packageSummaryMonth.getRemaining() >= 120){
							
							packageSummaryMonth.setExpended(packageSummaryMonth.getExpended() + calldetail.getTime());
							packageSummaryMonth.setRemaining(packageSummaryMonth.getRemaining() - calldetail.getTime());
							packageSummaryService.update(packageSummaryMonth);
							
							PackageSummary packageSummaryDay = packageSummaryService.getByDate(packag, day);
							if(packageSummaryDay==null){
								packageSummaryDay = new PackageSummary();
								packageSummaryDay.setCircuit(calldetail.getCircuit());
								packageSummaryDay.setPackag(packag);
								packageSummaryDay.setCalldate(month);
								packageSummaryDay.setExpended(packageSummaryDay.getExpended() + calldetail.getTime());
								packageSummaryDay.setRemaining(packageSummaryDay.getRemaining() - calldetail.getTime());
								packageSummaryService.update(packageSummaryDay);
							}
							else{
								packageSummaryDay.setExpended(packageSummaryDay.getExpended() + calldetail.getTime());
								packageSummaryDay.setRemaining(packageSummaryDay.getRemaining() - calldetail.getTime());
								packageSummaryService.update(packageSummaryDay);
							}			
							callLogger.calllog(calldetail, "DEBUG", "calldetailJOB", "Updating CallDetai with no Cost");
							calldetail.setSellrate(0.0);
							calldetail.setSellcost(0.0);
							callDetailService.update(calldetail);
							
						}
						else{
							callLogger.calllog(calldetail, "DEBUG", "calldetailJOB", "This package dont have free minutes");
							if(packag.getAditionalminutes()){
								if(packag.getAditionalrate().equals("package")){
									callLogger.calllog(calldetail, "DEBUG", "calldetailJOB", "Aditional Minutes at Package Rate");
									Integer seconds = calldetail.getTime();
									Double minutes = seconds.doubleValue()/60;
									Double sellrate = offer.getRate();
									sellrate = Math.round( sellrate * 1000.0 ) / 1000.0;
									Double sellcost = sellrate * minutes;
									sellcost = Math.round( sellcost * 10000.0 ) / 10000.0;
									calldetail.setSellrate(sellrate);
									calldetail.setSellcost(sellcost);
									callDetailService.update(calldetail);
								}
							}
						}
						
					}					
					break;	
				}		
				
			}//while package	
		
		}//else
		return calldetail;
		

	}//updatePackageUsage()

	
	public void updateCallSummaryRouteGroup(CallDetail calldetail) {
		
		Route route = calldetail.getRoute();
		Date month = dateUtil.round2Month(calldetail.getCalldate());
		Date day = dateUtil.round2Day(calldetail.getCalldate());
		
		CallSummaryRouteGroup callSummaryRouteGroupMonth = callSummaryRouteGroupService.getByDate(month, calldetail.getCircuit(), route.getRoutegroup());
		
		if(callSummaryRouteGroupMonth == null){
			callSummaryRouteGroupMonth = new CallSummaryRouteGroup();
			callSummaryRouteGroupMonth.setCircuit(calldetail.getCircuit());
			callSummaryRouteGroupMonth.setRoutegroup(route.getRoutegroup());
			callSummaryRouteGroupMonth.setCalldate(month);
			callSummaryRouteGroupMonth.setTime(calldetail.getTime());
			callSummaryRouteGroupMonth.setCost(calldetail.getSellcost());
			callSummaryRouteGroupMonth.setCalls(1);			
			callSummaryRouteGroupService.save(callSummaryRouteGroupMonth);
		
		}
		else{

			callSummaryRouteGroupMonth.setCalls(callSummaryRouteGroupMonth.getCalls() + 1);
			callSummaryRouteGroupMonth.setTime(callSummaryRouteGroupMonth.getTime() + calldetail.getTime());
			callSummaryRouteGroupMonth.setCost(callSummaryRouteGroupMonth.getCost() + calldetail.getSellcost());
			callSummaryRouteGroupService.update(callSummaryRouteGroupMonth);
			
			CallSummaryRouteGroup callSummaryRouteGroupDay = callSummaryRouteGroupService.getByDate(day, calldetail.getCircuit(), route.getRoutegroup());
			if(callSummaryRouteGroupDay == null){
				callSummaryRouteGroupDay = new CallSummaryRouteGroup();
				callSummaryRouteGroupDay.setCircuit(calldetail.getCircuit());
				callSummaryRouteGroupDay.setRoutegroup(route.getRoutegroup());
				callSummaryRouteGroupDay.setCalldate(day);
				callSummaryRouteGroupDay.setCalls(1);
				callSummaryRouteGroupDay.setTime(calldetail.getTime());
				callSummaryRouteGroupDay.setCost(calldetail.getSellcost());
				callSummaryRouteGroupService.save(callSummaryRouteGroupDay);
			}
			else{
				callSummaryRouteGroupDay.setCalls(callSummaryRouteGroupDay.getCalls() + 1);
				callSummaryRouteGroupDay.setTime(callSummaryRouteGroupDay.getTime() + calldetail.getTime());
				callSummaryRouteGroupDay.setCost(callSummaryRouteGroupDay.getCost() + calldetail.getSellcost());
				callSummaryRouteGroupService.update(callSummaryRouteGroupDay);
			}
			
		}
		
		
	}//updateCallSummary()


}
